package gps.map.test;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class MapSelect extends MapActivity {

	MyLocationOverlay myLoc;
	OverlayItem overlayitem;
	MyItemizedOverlay mOverlay;
	//ItemizedOverlay<OverlayItem> mOverlay;
	Drawable drawable;
	List<Overlay> mapOverlays;
	MapView selectMap;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        Overlay overlay=new Overlay() {
        @Override
        public boolean onTap(GeoPoint p, MapView mapView) {
     	   Context context = selectMap.getContext();
     	   //mapOverlays.remove(mOverlay);
     	   //mapOverlays.clear();
     	   mOverlay.removeOverlay(overlayitem);
     	   selectMap.invalidate();
     	   String msg = "Lat: " + p.getLatitudeE6()/1E6 + " - " + 
     	                "Lon: " + p.getLongitudeE6()/1E6;
     	   overlayitem = new OverlayItem(p, "Marker", "Do you wish to travel to this location?");
     	   mOverlay.addOverlay(overlayitem);
     	   alert(overlayitem,mOverlay,context);
     	   mapOverlays.add(mOverlay);
     	   //Toast toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
     	   //toast.show();
     	   return true;
        } 
		};
		
        String provider = LocationManager.GPS_PROVIDER;
        selectMap = (MapView) findViewById(R.id.mapView);
        selectMap.setBuiltInZoomControls(true);
        
        MapController mc = selectMap.getController();
		myLoc=new MyLocationOverlay(this, selectMap);
		mapOverlays = selectMap.getOverlays();
		
        drawable = this.getResources().getDrawable(R.drawable.marker);
		mOverlay = new MyItemizedOverlay(drawable,this);
		
		mapOverlays.add(overlay);

		mc.setZoom(16);
        mapOverlays.add(myLoc);
        myLoc.enableCompass();
        myLoc.enableMyLocation();
        
        
    }
	
    
    public void alert(final OverlayItem item, final MyItemizedOverlay mOverlay, Context context){
    	AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder
                .setMessage("Travel to this location?")
                .setCancelable(false).setPositiveButton("Yes!",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
								returnLocation();
                            }

							private GeoPoint returnLocation() {
								//mOverlay.removeOverlay(item);
								selectMap.invalidate();
								return item.getPoint();
							}
                        });
        builder.setNegativeButton("No!",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                }).create()
                  .show();
		}
	
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}	

}
